{
 "cells": [
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-10T06:05:38.922666Z",
     "start_time": "2025-09-10T06:05:38.916743Z"
    }
   },
   "cell_type": "code",
   "source": [
    "\n",
    "import pandas as pd\n",
    "path = 'D:/2506A/monty03/day12/file/'"
   ],
   "id": "2c07b3ee71b5ff3c",
   "outputs": [],
   "execution_count": 3
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# Pandas的数据结构",
   "id": "3343c6642fbdbbe0"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 1. Series : 一行或者一列",
   "id": "8b5db22d4b6216f1"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-09T08:35:13.823516Z",
     "start_time": "2025-09-09T08:35:13.818826Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data = pd.Series([1,'聂茹凤',21,'吉林省松原市','2004-5-13'])\n",
    "\n",
    "print(data)\n",
    "# 获取索引\n",
    "print(data.index)\n",
    "# 获取数据值\n",
    "print(data.values)"
   ],
   "id": "ae448809f3703ecb",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0            1\n",
      "1          聂茹凤\n",
      "2           21\n",
      "3       吉林省松原市\n",
      "4    2004-5-13\n",
      "dtype: object\n",
      "RangeIndex(start=0, stop=5, step=1)\n",
      "[1 '聂茹凤' 21 '吉林省松原市' '2004-5-13']\n"
     ]
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 2. 我们指定Series的索引",
   "id": "241b1fbf4328a45"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-09T08:37:05.284282Z",
     "start_time": "2025-09-09T08:37:05.278687Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data = pd.Series(\n",
    "    [1,'聂茹凤',21,'吉林省松原市','2004-5-13'],\n",
    "    index=['a','b','c','d','e']\n",
    ")\n",
    "\n",
    "print(data)\n",
    "# 获取索引\n",
    "print(data.index)\n",
    "# 获取数据值\n",
    "print(data.values)"
   ],
   "id": "4cf156c7019b3c9a",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a            1\n",
      "b          聂茹凤\n",
      "c           21\n",
      "d       吉林省松原市\n",
      "e    2004-5-13\n",
      "dtype: object\n",
      "Index(['a', 'b', 'c', 'd', 'e'], dtype='object')\n",
      "[1 '聂茹凤' 21 '吉林省松原市' '2004-5-13']\n"
     ]
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 3. 使用Python字典创建Series",
   "id": "26e727c5521b8d6e"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-09T08:43:26.238672Z",
     "start_time": "2025-09-09T08:43:26.233798Z"
    }
   },
   "cell_type": "code",
   "source": [
    "dict1 = {\n",
    "    '姓名':'谭鑫宇',\n",
    "    '性别':'女',\n",
    "    '年龄':20,\n",
    "    '地址':'吉林省长春市'\n",
    "}\n",
    "person = pd.Series(dict1)\n",
    "\n",
    "print(person)\n",
    "print(person.index) # 索引就是key\n"
   ],
   "id": "71c2d2e50d03f278",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名       谭鑫宇\n",
      "性别         女\n",
      "年龄        20\n",
      "地址    吉林省长春市\n",
      "dtype: object\n",
      "Index(['姓名', '性别', '年龄', '地址'], dtype='object')\n"
     ]
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 4. 根据索引查询Serice数据",
   "id": "c5b2073d56863eb5"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-09T08:45:05.154047Z",
     "start_time": "2025-09-09T08:45:05.148791Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(person['姓名'])\n",
    "print(type(person['年龄']))\n",
    "\n",
    "# 通过多个key 查询对应的值\n",
    "print(person[['姓名','地址']])\n",
    "print(type(person[['姓名','地址']])) # Series"
   ],
   "id": "4e1d7ce226884176",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "谭鑫宇\n",
      "<class 'int'>\n",
      "姓名       谭鑫宇\n",
      "地址    吉林省长春市\n",
      "dtype: object\n",
      "<class 'pandas.core.series.Series'>\n"
     ]
    }
   ],
   "execution_count": 23
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 5. 键和值存放在两个列表中创建Series",
   "id": "6bed348250c342b8"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-09T08:51:34.744988Z",
     "start_time": "2025-09-09T08:51:34.739329Z"
    }
   },
   "cell_type": "code",
   "source": [
    "keys = ['姓名','性别','年龄','地址']\n",
    "values = ['韩耀祖','男',22,None]\n",
    "\n",
    "data = pd.Series(values,index=keys)\n",
    "print(data)\n",
    "\n",
    "# 查看是否为空\n",
    "print(data.isnull())\n",
    "# 查看不为空\n",
    "print(data.notnull())\n",
    "\n"
   ],
   "id": "1648317bf4d7b55d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名     韩耀祖\n",
      "性别       男\n",
      "年龄      22\n",
      "地址    None\n",
      "dtype: object\n",
      "姓名    False\n",
      "性别    False\n",
      "年龄    False\n",
      "地址     True\n",
      "dtype: bool\n",
      "姓名     True\n",
      "性别     True\n",
      "年龄     True\n",
      "地址    False\n",
      "dtype: bool\n"
     ]
    }
   ],
   "execution_count": 29
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-09T08:54:53.889809Z",
     "start_time": "2025-09-09T08:54:53.881576Z"
    }
   },
   "cell_type": "code",
   "source": [
    "keys = [3,5,8,2,1]\n",
    "values = ['宋江','卢俊义','吴用','公孙胜','关胜']\n",
    "\n",
    "data = pd.Series(values,index=keys)\n",
    "print(data)\n",
    "print('按照索引排序')\n",
    "print(data.sort_index())\n",
    "\n",
    "print('按照values排序')\n",
    "print(data.sort_values())"
   ],
   "id": "1be5a883b69bf5b4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3     宋江\n",
      "5    卢俊义\n",
      "8     吴用\n",
      "2    公孙胜\n",
      "1     关胜\n",
      "dtype: object\n",
      "按照索引排序\n",
      "1     关胜\n",
      "2    公孙胜\n",
      "3     宋江\n",
      "5    卢俊义\n",
      "8     吴用\n",
      "dtype: object\n",
      "按照values排序\n",
      "2    公孙胜\n",
      "1     关胜\n",
      "5    卢俊义\n",
      "8     吴用\n",
      "3     宋江\n",
      "dtype: object\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# DataFrame",
   "id": "b39def11ceb1b18a"
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 1. 创建一个DataFrame",
   "id": "81c671fccc37a394"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-09T09:12:53.736085Z",
     "start_time": "2025-09-09T09:12:53.726639Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data = {\n",
    "    '姓名':['聂茹凤','谭鑫宇','韩耀祖'],\n",
    "    '年龄':[21,22,23],\n",
    "    '地址':['吉林省松原市','吉林省松原市','河北省邯郸市']\n",
    "}\n",
    "\n",
    "df = pd.DataFrame(data,index=[1,2,3])\n",
    "\n",
    "print(df)\n",
    "# 获取谭鑫宇\n",
    "print(df['姓名'][1])\n",
    "\n",
    "# print(df[1]['姓名']) 报错\n",
    "# 使用loc 获取指定行列的数据\n",
    "print(df.loc[1]['姓名'])\n",
    "# print(df.loc[1,0]) # 报错 可以使用iloc\n",
    "\n",
    "print('=' * 30)\n",
    "print(df.iloc[0]['姓名'])\n",
    "print(df.iloc[0,0])\n",
    "\n",
    "print('=' * 30)\n",
    "\n",
    "# 通过 列名来访问\n",
    "print(df[['姓名','地址']])\n",
    "# print(df[[1,2]])  # 报错，这种方式不可以通过索引来访问\n",
    "\n",
    "print(df.index)\n",
    "\n",
    "\n"
   ],
   "id": "d6c2e2df2c393e48",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    姓名  年龄      地址\n",
      "1  聂茹凤  21  吉林省松原市\n",
      "2  谭鑫宇  22  吉林省松原市\n",
      "3  韩耀祖  23  河北省邯郸市\n",
      "聂茹凤\n",
      "聂茹凤\n",
      "==============================\n",
      "聂茹凤\n",
      "聂茹凤\n",
      "==============================\n",
      "    姓名      地址\n",
      "1  聂茹凤  吉林省松原市\n",
      "2  谭鑫宇  吉林省松原市\n",
      "3  韩耀祖  河北省邯郸市\n",
      "Index([1, 2, 3], dtype='int64')\n"
     ]
    }
   ],
   "execution_count": 56
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-09T09:13:37.049893Z",
     "start_time": "2025-09-09T09:13:37.045620Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 查看df的信息\n",
    "print(df.dtypes)    # 返回每一列的类型\n",
    "print(df.columns) # 返回列索引，以列表形式返回：[列名1，列名2，…]\n",
    "print(df.index)      # 返回行索引，（起始，结束，步长）\n",
    "print(df.shape)     # 形状(行数, 列数)"
   ],
   "id": "e7c048b210906393",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "姓名    object\n",
      "年龄     int64\n",
      "地址    object\n",
      "dtype: object\n",
      "Index(['姓名', '年龄', '地址'], dtype='object')\n",
      "Index([1, 2, 3], dtype='int64')\n",
      "(3, 3)\n"
     ]
    }
   ],
   "execution_count": 57
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 2. 从DataFrame中查询出Series",
   "id": "219cf7b40b8207e6"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-09T09:19:39.359120Z",
     "start_time": "2025-09-09T09:19:39.353902Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# print(df['姓名']) # 查询一列\n",
    "# 查询一行\n",
    "# print(df.loc[1]) # 找下标\n",
    "# print(df.iloc[0]) # 找行号\n",
    "\n",
    "# 查找多列\n",
    "# print(df[['姓名','地址']])\n",
    "\n",
    "# 查询多行\n",
    "# print(df.loc[1:2]) # 找索引，包含2\n",
    "print(df.iloc[1:3]) # 找行号，行号从0开始，不包含3"
   ],
   "id": "8011be2ea04d3297",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    姓名  年龄      地址\n",
      "2  谭鑫宇  22  吉林省松原市\n",
      "3  韩耀祖  23  河北省邯郸市\n"
     ]
    }
   ],
   "execution_count": 69
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "## 3. 将多个Series添加到df中",
   "id": "20b302beb62dbb34"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-09T09:26:07.987975Z",
     "start_time": "2025-09-09T09:26:07.978402Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data1 = pd.Series(['聂茹凤','谭鑫宇','韩耀祖'],index=[1,2,3],name='姓名')\n",
    "data2 = pd.Series(['女','女','男'],index=[1,2,3],name='性别')\n",
    "data3 = pd.Series(['吉林松原','吉林长春','河北邯郸'],index=[1,2,3],name='地址')\n",
    "\n",
    "# 作为列添加\n",
    "df1 = pd.DataFrame({data1.name:data1,data2.name:data2,data3.name:data3})\n",
    "print(df1)\n",
    "\n",
    "print('=' * 30)\n",
    "\n",
    "# 作为行添加\n",
    "df2 = pd.DataFrame([data1,data2,data3])\n",
    "print(df2)"
   ],
   "id": "6339c8415d8a5b62",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    姓名 性别    地址\n",
      "1  聂茹凤  女  吉林松原\n",
      "2  谭鑫宇  女  吉林长春\n",
      "3  韩耀祖  男  河北邯郸\n",
      "==============================\n",
      "       1     2     3\n",
      "姓名   聂茹凤   谭鑫宇   韩耀祖\n",
      "性别     女     女     男\n",
      "地址  吉林松原  吉林长春  河北邯郸\n"
     ]
    }
   ],
   "execution_count": 71
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-10T06:22:01.544364Z",
     "start_time": "2025-09-10T06:22:01.482018Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 往学生信息4中添加以一行数据\n",
    "data = pd.read_excel(path + '学生信息4.xlsx')\n",
    "# 写入一行\n",
    "# row = pd.Series(['曹操',33,'河南许昌'])\n",
    "# data.iloc[3] = row\n",
    "\n",
    "# loc是通过索引来锁定位置的，默认索引是[0,1,2],需要指定和表相同的索引才能正确插入数据\n",
    "# row = pd.Series(['曹操',33,'河南许昌'],index=data.columns)\n",
    "# data.loc[3] = row\n",
    "# data.loc[len(data)] = row\n",
    "\n",
    "# 添加性别列\n",
    "col = pd.Series(['女','女','男','男','女','男','女'],index=[0,1,2,3,4,5,6],name='性别')\n",
    "\n",
    "data['性别'] = col\n",
    "\n",
    "print(data)\n",
    "data.to_excel(path + '学生信息4.xlsx',index=False)"
   ],
   "id": "268149a72c5a3a45",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "    姓名  年龄      地址 性别\n",
      "0  聂茹凤  21  吉林省松原市  女\n",
      "1  谭鑫宇  22  吉林省松原市  女\n",
      "2  韩耀祖  23  河北省邯郸市  男\n",
      "3   曹操  33    河南许昌  男\n",
      "4  刘千琪  21  吉林省松原市  女\n",
      "5  崔龙腾  22  吉林省松原市  男\n",
      "6  李欣桐  23  河北省邯郸市  女\n"
     ]
    }
   ],
   "execution_count": 24
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "## 4. DataFrame常用方法\n",
    "-  数据.head( 5 ) #查看前5行\n",
    "-  数据.tail( 3 ) #查看后3行\n",
    "- 数据.values #查看数值\n",
    "- 数据shape #查看行数、列数\n",
    "- 数据.fillna(0) #将空值填充0\n",
    "- 数据.replace( 1, -1) #将1替换成-1\n",
    "- 数据.isnull() #查找数据中出现的空值\n",
    "- 数据.notnull() #非空值\n",
    "- 数据.dropna() #删除空值\n",
    "- 数据.unique() #查看唯一值\n",
    "- 数据.reset_index() #修改、删除，原有索引，详见例1\n",
    "- 数据.columns #查看数据的列名\n",
    "- 数据.index #查看索引\n",
    "- 数据.sort_index() #索引排序\n",
    "- 数据.sort_values() #值排序\n",
    "- pd.merge(数据1,数据1) #合并\n",
    "- pd.concat([数据1,数据2]) #合并，与merge的区别，自查\n",
    "- pd.pivot_table( 数据 ) #用df做数据透视表（类似于Excel的数透）\n"
   ],
   "id": "64221aae4a8e77bf"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-10T06:25:58.489035Z",
     "start_time": "2025-09-10T06:25:58.475396Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(f'前三行 {data.head(3)}')\n",
    "print(f'后三行：{data.tail(3)}')\n",
    "print(f'查看值:{data.values}')\n",
    "print(f'查看几行几列:{data.shape}')"
   ],
   "id": "7ec25df1cf414363",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "前三行     姓名  年龄      地址 性别\n",
      "0  聂茹凤  21  吉林省松原市  女\n",
      "1  谭鑫宇  22  吉林省松原市  女\n",
      "2  韩耀祖  23  河北省邯郸市  男\n",
      "后三行：    姓名  年龄      地址 性别\n",
      "4  刘千琪  21  吉林省松原市  女\n",
      "5  崔龙腾  22  吉林省松原市  男\n",
      "6  李欣桐  23  河北省邯郸市  女\n",
      "查看值:[['聂茹凤' 21 '吉林省松原市' '女']\n",
      " ['谭鑫宇' 22 '吉林省松原市' '女']\n",
      " ['韩耀祖' 23 '河北省邯郸市' '男']\n",
      " ['曹操' 33 '河南许昌' '男']\n",
      " ['刘千琪' 21 '吉林省松原市' '女']\n",
      " ['崔龙腾' 22 '吉林省松原市' '男']\n",
      " ['李欣桐' 23 '河北省邯郸市' '女']]\n"
     ]
    }
   ],
   "execution_count": 26
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-10T06:33:39.379272Z",
     "start_time": "2025-09-10T06:33:39.363594Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 演示- pd.merge(数据1,数据1) #合并\n",
    "# 创建第一个 DataFrame\n",
    "data1 = pd.DataFrame({\n",
    "    '编号': [1, 2, 3, 4, 5, 6, 7],\n",
    "    '姓名': ['刘备', '诸葛亮', '关羽', '张飞', '赵云', '黄忠', '马超'],\n",
    "    '年龄': [43, 21, 43, 43, 33, 78, 43],\n",
    "    '别名': ['玄德', '孔明', '云长', '翼德', '子龙', '汉升', '孟起'],\n",
    "    '武器': ['双股剑', '羽扇', '青龙偃月刀', '丈八蛇矛', '龙胆亮银枪', '赤血刀', '虎头湛金枪']\n",
    "})\n",
    "# 创建第二个 DataFrame\n",
    "data2 = pd.DataFrame({\n",
    "    '编号': [1, 2, 3, 4, 5, 6, 7],\n",
    "    '姓名': ['刘备', '诸葛亮', '关羽', '张飞', '赵云', '黄忠', '马超'],\n",
    "    '别名': ['玄德', '孔明', '云长', '翼德', '子龙', '汉升', '孟起'],\n",
    "    '称号': ['蜀汉昭烈帝', '卧龙', '武圣', '猛张飞', '常胜将军', '老当益壮', '锦马超']\n",
    "})\n",
    "\n",
    "# 根据 '姓名' 和 '别名' ,'编号'   列进行合并\n",
    "e = data1.merge(data2,on=['姓名','编号','别名'])\n",
    "print(e)\n"
   ],
   "id": "e18df3b1694bfd1c",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   编号   姓名  年龄  别名     武器     称号\n",
      "0   1   刘备  43  玄德    双股剑  蜀汉昭烈帝\n",
      "1   2  诸葛亮  21  孔明     羽扇     卧龙\n",
      "2   3   关羽  43  云长  青龙偃月刀     武圣\n",
      "3   4   张飞  43  翼德   丈八蛇矛    猛张飞\n",
      "4   5   赵云  33  子龙  龙胆亮银枪   常胜将军\n",
      "5   6   黄忠  78  汉升    赤血刀   老当益壮\n",
      "6   7   马超  43  孟起  虎头湛金枪    锦马超\n"
     ]
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-10T06:35:39.704031Z",
     "start_time": "2025-09-10T06:35:39.689056Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 演示  pd.concat([数据1,数据2]) #合并，与merge的区别，自查\n",
    "# 创建第一个 DataFrame\n",
    "data1 = pd.DataFrame({\n",
    "    '编号': [1, 2, 3],\n",
    "    '姓名': ['刘备', '诸葛亮', '关羽'],\n",
    "    '年龄': [43, 21, 43],\n",
    "    '别名': ['玄德', '孔明', '云长'],\n",
    "    '武器': ['双股剑', '羽扇', '青龙偃月刀']\n",
    "})\n",
    "# 创建第二个 DataFrame\n",
    "data2 = pd.DataFrame({\n",
    "    '编号': [4, 5, 6],\n",
    "    '姓名': ['张飞', '赵云', '黄忠'],\n",
    "    '年龄': [43, 33, 78],\n",
    "    '别名': ['翼德', '子龙', '汉升'],\n",
    "    '武器': ['丈八蛇矛', '龙胆亮银枪', '赤血刀']\n",
    "})\n",
    "# 将两个 DataFrame 沿行进行连接\n",
    "e = pd.concat([data1, data2],axis=0)\n",
    "print(e)\n"
   ],
   "id": "2d71d7ba41950230",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   编号   姓名  年龄  别名     武器\n",
      "0   1   刘备  43  玄德    双股剑\n",
      "1   2  诸葛亮  21  孔明     羽扇\n",
      "2   3   关羽  43  云长  青龙偃月刀\n",
      "0   4   张飞  43  翼德   丈八蛇矛\n",
      "1   5   赵云  33  子龙  龙胆亮银枪\n",
      "2   6   黄忠  78  汉升    赤血刀\n"
     ]
    }
   ],
   "execution_count": 34
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-10T06:39:43.335368Z",
     "start_time": "2025-09-10T06:39:43.314338Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 演示 - pd.pivot_table( 数据 ) #用df做数据透视表（类似于Excel的数透）\n",
    "# 创建一个包含销售数据的 DataFrame\n",
    "data = pd.DataFrame({\n",
    "    '产品': ['A', 'A', 'B', 'B', 'A', 'B', 'A', 'B'],\n",
    "    '地区': ['东区', '西区', '东区', '西区', '东区', '西区', '东区', '西区'],\n",
    "    '销售额': [100, 150, 200, 250, 300, 350, 400, 450]\n",
    "})\n",
    "# print(data)\n",
    "# 生成数据透视表\n",
    "e = pd.pivot_table(data,values='销售额',index='产品',columns='地区',aggfunc='sum')\n",
    "print(e)"
   ],
   "id": "6a2cf4d067ea2a8",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "地区   东区   西区\n",
      "产品          \n",
      "A   400  150\n",
      "B   200  450\n"
     ]
    }
   ],
   "execution_count": 38
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
